// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioMgr from "./common/AudioMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {


    _set_view: fgui.GComponent;

    _music_btn: fgui.GButton;

    _verbtion_btn: fgui.GButton;

    // onLoad () {}
    onLoad() {
        fgui.addLoadHandler();

        fgui.GRoot.create();

        fgui.UIPackage.loadPackage("UI/IMGame", this.onUILoaded.bind(this));
    }

    onUILoaded() {
        // fgui.UIPackage.addPackage("UI/IMGame");

        this._set_view = fgui.UIPackage.createObject("IMGame", "SettingPanel").asCom;
        this._set_view.makeFullScreen();
        // this._integral_view.visible=false;

        //100  打开  101 关闭  select=true时显示off
        this._music_btn = this._set_view.getChild("n31").asButton;
        this._music_btn.onClick(this.onMusicClick, this);
        this._verbtion_btn = this._set_view.getChild("n32").asButton;
        this._verbtion_btn.onClick(this.onVerbtionClick, this);
        this._set_view.getChild("n34").onClick(this.onBack, this);
        this.setData();

        fgui.GRoot.inst.addChild(this._set_view);
    }

    onBack() {
        cc.director.loadScene("start");
    }

    onMusicClick() {
        //select=true off 101
        if (this._music_btn.selected) {
            cc.sys.localStorage.setItem('bgVoice', "101");
            // this._music_btn.selected = false;
            gg.audioMgr.stopBGM();
        } else {
            //select=false on 100
            cc.sys.localStorage.setItem('bgVoice', "100");
            // this._music_btn.selected = true;
        }
    }

    onVerbtionClick() {
        if (this._verbtion_btn.selected) {
            cc.sys.localStorage.setItem('verbtion', "101");
            // this._verbtion_btn.selected = false;
        } else {
            cc.sys.localStorage.setItem('verbtion', "100");
            // this._verbtion_btn.selected = true;
        }
    }

    setData() {
        //获取数据
        if (cc.sys.localStorage.getItem('bgVoice') === null) {
            //没有设置过

            //保存数据 100 播放  101 不播放
            cc.sys.localStorage.setItem('bgVoice', "100");
            this._music_btn.selected = false;

            // gg.audioMgr.playBGM("piano");
        } else {
            //有设置

            if (cc.sys.localStorage.getItem('bgVoice') === "100") {
                this._music_btn.selected = false;
            } else {
                this._music_btn.selected = true;
            }
        }

        if (cc.sys.localStorage.getItem('verbtion') === null) {
            //没有设置过

            //保存数据 100 播放  101 不播放
            cc.sys.localStorage.setItem('verbtion', "100");
            this._verbtion_btn.selected = false;

            // gg.audioMgr.playBGM("piano");
        } else {
            //有设置

            if (cc.sys.localStorage.getItem('verbtion') === "100") {
                this._verbtion_btn.selected = false;
            } else {
                this._verbtion_btn.selected = true;
            }
        }
    }

    start() {

    }

    // update (dt) {}
}
